package com.yuedou.api.service.platfom.impl;

import cn.hutool.json.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.yuedou.api.dao.platform.SmsTemplateMapper;
import com.yuedou.api.entity.platform.SmsTemplate;
import com.yuedou.api.entity.wxuser.User;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.platfom.SmsTemplateService;
import com.yuedou.api.util.SendSms;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SmsTemplateServiceImpl extends BaseServiceImpl<SmsTemplateMapper, SmsTemplate> implements SmsTemplateService {

    @Resource
    private SmsTemplateMapper smsTemplateMapper;

    @Override
    public void sendSmsByGetEggToManager(String merName,String phoneNum, User user, int eggCount,String time) {
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",0);
        if(sms!=null){
//        ${mer}管理员您好，${name}于${time} 领取鸡蛋${count}个，请及时处理。感谢您的使用，祝您生意兴隆~
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("mer", merName);
            smsJson.set("name", user.getNickName());
            smsJson.set("time", time);
            smsJson.set("count", eggCount);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendSmsByProductOrder(String merName, String phoneNum, String productName) {
        //${name}管理员您好，新订单来喽~, 用户下单${product}。感谢您的使用，祝您生意兴隆~
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",1);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("name", merName);
            smsJson.set("product", productName);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendSmsByProductOrderVisit(String merName, String phoneNum, String productName) {
        //${name}管理员您好，新订单来喽~, 用户下单${product}，请尽快联系客户处理。感谢您的使用，祝您生意兴隆~
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",2);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("name", merName);
            smsJson.set("product", productName);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public String sendSmsByPhoneCode(String code, String phoneNum) {
        //您的验证码为：${code}，请勿泄露于他人！
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",3);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("code", code);
            try {
                SendSmsResponse response =sendSms.send(phoneNum,smsJson,sms);
                System.out.println("验证码响应：" + response.toString());
                return response.getCode();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return "FAIL";
    }
}
